﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Partial Class Reports_report_log_system2
    Inherits System.Web.UI.Page
    'Create report engine
    Private report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        '        report = New CrystalDecisions.CrystalReports.Engine.ReportDocument
        'Show report
        CrystalReportViewer1.ReportSource = report
        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dw As report_log_systemTableAdapters.USP_List_log_system_reportTableAdapter
        Dim dt As DataTable
        Dim strLogType As String
        Dim strLogmenu As String
        Dim strStartDate As String
        Dim strEndDate As String
        Dim ifiltertype As Integer
        Dim strFirstname As String
        Dim strLastname As String
        Dim strStaffNo As String
        Dim str_Staff As String
        Dim iDepartmentCode As Integer
        Dim strDepartmentName As String
        Dim strLogTypeName As String
        Dim strLogMenuName As String
        Dim iCurrentStaffCode As Integer
        Dim type As String
        Try
            'get Querystring
            If Request.QueryString.Count > 0 Then

                iCurrentStaffCode = Session("staff_code")
                iDepartmentCode = Request.QueryString("department_code")
                strLogmenu = Request.QueryString("log_menu")
                strLogType = Request.QueryString("log_type")

                If (Request.QueryString("start_date") = "") Then
                    strStartDate = (Date.Now.ToString("yyyy/MM/dd", New System.Globalization.CultureInfo("en-US")))
                Else
                    strStartDate = Request.QueryString("start_date") 'Date.Parse(Request.QueryString("start_date")).ToString("yyyy/MM/dd", New System.Globalization.CultureInfo("en-US"))
                End If

                If (Request.QueryString("end_date") = "") Then
                    strEndDate = (Date.Now.ToString("yyyy/MM/dd", New System.Globalization.CultureInfo("en-US")))
                Else
                    strEndDate = Request.QueryString("end_date") 'Date.Parse(Request.QueryString("end_date")).ToString("yyyy/MM/dd", New System.Globalization.CultureInfo("en-US"))
                End If

                ifiltertype = Request.QueryString("filterType")
                strLastname = Request.QueryString("staff_lastname")
                strFirstname = Request.QueryString("staff_firstname")
                strStaffNo = Request.QueryString("staff_no")
                type = Request.QueryString("type")

                If (type = "0") Then
                    strDepartmentName = Request.QueryString("str_depart_name")
                    strDepartmentName = IIf(strDepartmentName = "หน่วยงานที่สังกัด", "ทั้งหมด", strDepartmentName)

                    strLogTypeName = Request.QueryString("str_log_type")
                    strLogTypeName = IIf(strLogTypeName = "ประเภท", "ทั้งหมด", strLogTypeName)
                    str_Staff = IIf(Trim(strFirstname) + Trim(strLastname) = "", "ทั้งหมด", strFirstname + " " + strLastname)
                Else
                    strLogTypeName = Request.QueryString("str_log_type")
                    strLogTypeName = IIf(strLogTypeName = "ประเภท", "ทั้งหมด", strLogTypeName)

                    strLogMenuName = Request.QueryString("str_log_menu")
                    strLogMenuName = IIf(strLogMenuName = "ระบบงาน", "ทั้งหมด", strLogMenuName)
                    str_Staff = IIf(Request.QueryString("str_staff") = "", "ทั้งหมด", Request.QueryString("str_staff"))

                End If

                dw = New report_log_systemTableAdapters.USP_List_log_system_reportTableAdapter

                'Retrieve data from DB & collect to datatable
                dt = dw.GetData(iDepartmentCode, strLogmenu, strLogType, strStartDate, strEndDate, ifiltertype, strFirstname, strLastname, strStaffNo, iCurrentStaffCode)
                'check 
                If (dt.Rows.Count > 0) Then
                   
                    'map report parth
                    If type = "0" Then
                        report.Load(Server.MapPath("report_log_system.rpt"))
                    Else
                        report.Load(Server.MapPath("report_log_system2.rpt"))
                    End If
                    'set data table to report
                    report.SetDataSource(dt)
                    'set input paratmenter
                    report.SetParameterValue("LogType", strLogTypeName)
                    If type = "0" Then
                        report.SetParameterValue("DepartmentName", strDepartmentName)
                    Else
                        report.SetParameterValue("MenuName", strLogMenuName)

                    End If

                    report.SetParameterValue("EndDate", strEndDate)
                    report.SetParameterValue("StartDate", strStartDate)
                    report.SetParameterValue("StaffName", str_Staff)

                  
                Else
                    ' hide invalid message
                    CrystalReportViewer1.Visible = False

                    GenericHelper.ShowAlert(Me, "ไม่พบข้อมูล")
                    Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                    If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                        Page.RegisterStartupScript("clientScript", strscript)
                    End If
                    ' TODO:
                    ' Redirect to pervious page
                End If
            Else
                ' hide invalid message
                CrystalReportViewer1.Visible = False

                GenericHelper.ShowAlert(Me, "ไม่ถูกต้อง")
                Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", strscript)
                End If

            End If
        Catch ex As Exception
            ' hide invalid message
            CrystalReportViewer1.Visible = False

            GenericHelper.ShowAlert(Me, ex.Message)
            Dim strscript As String = "<script language=javascript>window.top.close();</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strscript)
            End If
        Finally
            If Not (dt Is Nothing) Then
                dt.Dispose()
            End If
            If Not (dw Is Nothing) Then
                dw.Dispose()
            End If
        End Try
    End Sub

    'Change formdate from "yyyy-mm-dd" to "dd-mm-yyyy"
    Private Function ChangeFormatDate(ByVal strDate As String) As String
        Dim dateSplit() As String
        dateSplit = strDate.Split("-")
        If dateSplit.Length > 2 Then
            Return dateSplit(2) + "/" + dateSplit(1) + "/" + dateSplit(0)
        Else
            Return strDate
        End If
    End Function


    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        If Not (CrystalReportViewer1 Is Nothing) Then
            CrystalReportViewer1.Dispose()
        End If
    End Sub

    Protected Sub CrystalReportViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Unload
        If Not (report Is Nothing) Then
            report.Close()
            report.Dispose()
        End If
    End Sub
End Class
